package com.vpen.blog.controller;

import com.vpen.blog.common.ReturnResult;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * -@Version 1.0
 * -@Author:Vpen
 * -@Date:2021/7/7 21:49
 * -@Content: 验证码
 */
@RestController
@RequestMapping("api/captcha")
public class CaptchaController {

    /**
     * 验证码生成
     * @param request 请求报文
     * @param response 响应报文
     * */
    @RequestMapping("/captchaImage")
    public void generate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        GifCaptcha gifCaptcha = new GifCaptcha(130,48,4);
        CaptchaUtil.out(gifCaptcha, request, response);
    }

    /**
     * 异步验证验证码
     * @param request 请求报文
     * @return 验证结果
     * */
    @RequestMapping("/verify")
    public ReturnResult verify(@RequestBody Map<String,Object> map, HttpServletRequest request){
        String code = (String) map.get("code");
        if(!CaptchaUtil.ver(code,request)){
            return ReturnResult.getInstance().fail("验证码错误");
        }
        return null;
    }



}
